<template>
    <div class="components_edit_modal">
        <el-dialog title="审核流程" :visible.sync="state"  :close-on-click-modal="false">
            <el-form :model="form"   :rules="rules" ref="form">
                <el-form-item label="标题：" class="lineblock">
                     {{ subject }}
                </el-form-item>
                
                <el-form-item label="当前节点：">
                    {{ currentNodeName }}
                </el-form-item>

                <el-form-item label="审核状态：" prop="flowState">
                    <el-select v-model="form.flowState" placeholder="请选择审核状态" @change="changeFlowState">
                        <el-option
                            v-for="item in flowStateList" :key="item.id"
                            :label="item.text"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                 <el-form-item label="下级节点："  v-if="nextNodeId != null && processPoint == 'next'">
                    {{ nextProcess.nodeName }}
                </el-form-item>

                <el-form-item label="下级节点："  v-if="processPoint == 'prev'">
                    {{ prevProcess.nodeName }}
                </el-form-item>

                <el-form-item label="审批人："  v-if="nextNodeId != null && processPoint == 'next'">
                    <el-select v-model="form.toUserId" placeholder="请选择审批人">
                        <el-option
                            v-for="item in userList" :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                 <el-form-item label="审批人："  v-if="processPoint == 'prev'">
                   {{ prevProcess.userName }}
                </el-form-item>

                <el-form-item label="审批意见：">
                    <el-input
                            type="textarea"
                            :maxlength="500"
                            :autosize="{ minRows: 2, maxRows:4}"
                            placeholder="请输入审批意见"
                            v-model="form.optionContent">
                    </el-input>
                </el-form-item>
               
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submit" size="small">确 定</el-button>
                <el-button @click="state = false" size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums,constant} from '@/constant'
    export default {
        data() {
            return {
                businessCode: null,
                subject: null,
                nextNodeId: null,
                currentNodeName: null,
                businessId: null,
                processPoint:null,
                rules: {
                    flowState: [
                        { required: true, message: '请选择审核状态', trigger: 'change', type: 'number'},

                    ],
                },
                form: {
                    nodeId: null,    // 节点id
                    flowId: null,    // 流程id
                    flowState: null,   // 审核状态
                    toUserId:null,   // 提交下级审批人
                    optionContent: null,   // 审批意见
                },
                prevProcess: {},   // 上级流程对象
                nextProcess: {},   // 下级节点流程对象
                userList: null,    // 提交人数组集合
                flowStateList: null,    // 审核状态枚举集合
            }
        },
        created() {
           
        },
        mounted() {},
        computed:{
            state: {
                get: function () {
                    if(this.$store.getters.getFlowExamine.state == true && this.subject == null){
                        

                        // 界面所需要数据
                        let data = this.$store.getters.getFlowExamine.data;
                        this.subject = data.subject;
                        this.businessId = data.businessId;
                        this.businessCode = data.businessCode;
                        this.currentNodeName = data.nodeName;
                        this.nextNodeId = data.nextNodeId == null ? null : data.nextNodeId;
                         // 处理选项
                        if(data.nodeProcessState == 0){     // 起始流程
                            this.flowStateList = enums.FLOW_STATE.filter(item => item.id == 1);
                        }else{
                             // 移除待审核项
                            this.flowStateList = enums.FLOW_STATE.filter(item => item.id != 0);
                        }
                        // 表单所需数据
                        this.form.nodeId = data.nodeId;
                        this.form.flowId = data.id;
                        this.form.flowState = this.flowStateList[0].id;
                        this.form.toUserId = null;
                        // 获取流程下级节点
                         this.changeFlowState();
                        // 重置字段和校验
                        this.$nextTick(function(){
                            this.$refs['form'].resetFields();
                        })
                       
                    }
                    return this.$store.getters.getFlowExamine.state;
                },
                set: function (v) {
                   if(!v){
                        this.subject = null;
                        this.$store.commit("FLOW_EXAMINE", {dialogState: v, data: null});
                   }
                }
            } 
        },
        methods: {
            // 打开模态框
            showDialog(row){
                this.state = true;
                this.subject = row.subject;
                this.businessCode = row.businessCode;
                this.currentNodeName = row.nodeName;
                this.nextNodeId = row.nextNodeId == null ? null : row.nextNodeId;
                // 表单所需数据
                this.form.nodeId = row.nodeId;
                this.form.flowId = row.id;
                // 获取流程下级节点
                // this.getNextNode();

                this.$nextTick(function(){
                    this.$refs['form'].resetFields();
                })
            },
               // 查询流程下级节点
            getNextNode() {
                let vm = this;
                if(vm.nextNodeId == null){
                    return;
                }
                vm.axios.get(api.flow.queryProcessById +  vm.nextNodeId).then((response) => {
                    if (response.data.success) {
                        this.nextProcess = JSON.parse(JSON.stringify(response.data.data));
                        // 查询下级审批人列表
                        this.getUserList();
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 查询流程上级节点
            getPrevNode(){
                let vm = this;
                vm.axios.get(api.flow.queryPrevRecord ,{
                    params:{
                        businessId: vm.businessId,
                        nodeId: vm.form.nodeId
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.prevProcess = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
             // 获取用户集合
            getUserList() {
                let vm = this;
                vm.axios.get(api.queryUserListByRoleFlag, {
                    params:{
                        roleFlag: vm.nextProcess.userRule
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.userList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 保存
            submit() {
                let vm = this;
                vm.$refs['form'].validate((valid) => {
                    if (valid) {
                        if(vm.processPoint == "next"){
                            if(this.form.toUserId == null && this.nextNodeId != null){
                                 vm.$message({
                                    type: 'warn',
                                    message: '提交审核人不能为空！',
                                });
                                return;
                            }
                        }
                        // 请求
                        // console.log(this.form);
                        // return;
                        vm.axios.post(api.flow.examineFlow, vm.form)
                        .then((response) => {
                            if (response.data.success) {
                                vm.$message({
                                    type: 'success',
                                    message: response.data.msg,
                                });
                                // 关闭弹出窗口
                                vm.state = false;
                                // 刷新父级表格
                                vm.$parent.$refs.flowTable.$emit('vuetable:refresh');
                            } else {
                                vm.$message.error(response.data.msg);
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            changeFlowState(){    // 修改审核状态
                let vm = this; 
                let val = vm.form.flowState;
                if(val == constant.FLOW_STATE.APPROVED){     // 通过  查询下一节点
                    vm.processPoint = "next";
                    if(_.isEmpty(vm.nextProcess)){
                        vm.getNextNode();
                    }
                }else if(val == constant.FLOW_STATE.NOT_APPROVED){     // 不通过  查询上一节点
                    vm.processPoint = "prev";
                    if(_.isEmpty(vm.prevProcess)){
                        vm.getPrevNode();
                    }
                    
                }
            }
        },
    };
</script>
<style>
</style>